package com.crm.system.service.impl.opportunityManager;

import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.opportunityManager.Opportunity;
import com.crm.model.entity.opportunityManager.OpportunityCollaborator;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.opportunityManager.OpportunityQO;
import com.crm.model.vo.opportunityManager.OpportunityCollaboratorVO;
import com.crm.model.vo.opportunityManager.OpportunityVO;
import com.crm.service.opportunityManager.OpportunityCollaboratorDbService;
import com.crm.service.opportunityManager.OpportunityDbService;
import com.crm.system.service.opportunityManager.OpportunityCollaboratorService;
import com.crm.system.service.opportunityManager.OpportunityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * bj
 * 协作人
 */
@Service
public class OpportunityCollaboratorServiceImpl implements OpportunityCollaboratorService {

    @Autowired
    private OpportunityDbService opportunityDbService;

    @Autowired
    private OpportunityCollaboratorDbService opportunityCollaboratorDbService;

    /**
     * 商机-商机详情-查询所有用户
     * @return
     */
    @Override
    public List<OpportunityCollaboratorVO> userList(String opportunityId) {
        Opportunity opportunity = opportunityDbService.selectByOpportunityId(opportunityId);
        List<OpportunityCollaboratorVO> list =  opportunityCollaboratorDbService.userList(opportunity.getAccountId(),1);
        return list;
    }

    /**
     * 商机管理-新增协作人
     * @param opportunityCollaborator 商机协作人实体
     * @return
     */
    @Override
    public Boolean addOpportunityCollaborator(OpportunityCollaborator opportunityCollaborator) {
        Boolean result = false;
        OpportunityCollaborator opp = new OpportunityCollaborator();
        opp.setAccountId(opportunityCollaborator.getAccountId());
        opp.setOpportunityId(opportunityCollaborator.getOpportunityId());
        //通过用户id和商机id查询是否这个协作人之前是废止的吗
        OpportunityCollaborator oppo = opportunityCollaboratorDbService.queryIdAndOpportunityId(opp);
        if(!StringUtils.isBlank(oppo.getOpportunityCollaboratorId())){
            oppo.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
            result = opportunityCollaboratorDbService.updateOpportunityCollaborator(oppo);
        }else{
            String opportunityCollaboratorId = CommonUtils.getGUID();
            opportunityCollaborator.setOpportunityCollaboratorId(opportunityCollaboratorId);
            opportunityCollaborator.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
            result = opportunityCollaboratorDbService.addOpportunityCollaborator(opportunityCollaborator);
        }
        return result;

    }

    /**
     * 商机-商机详情-查询当前商机的协作人
     * @param opportunityId 商机id
     * @return
     */
    @Override
    public List<OpportunityCollaborator> opportunityCollaboratorList(String opportunityId) {
         List<OpportunityCollaborator> list = opportunityCollaboratorDbService.opportunityCollaboratorList(opportunityId);
         return list;
    }

    /**
     * 商机-商机详情-商机协作人-删除
     * @param opportunityCollaboratorId 协作人id
     * @return
     */
    @Override
    public Boolean deleteOpportunityCollaborator(String opportunityCollaboratorId) {
        OpportunityCollaborator opportunityCollaborator = new OpportunityCollaborator();
        opportunityCollaborator.setOpportunityCollaboratorId(opportunityCollaboratorId);
        opportunityCollaborator.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));
        return opportunityCollaboratorDbService.updateOpportunityCollaborator(opportunityCollaborator);
    }


}
